import config from "./config.js"
import {
	https
} from './https.js'
export const request = (options) => {
	let accessToken = uni.getStorageSync('accessToken') || '';	
	let refToken = uni.getStorageSync('refreshAccessToken') || '';	
	
	if (!accessToken) {
		return uni.redirectTo({
			url: '/pages/mine/login'
		})
	}
	return new Promise((resolve, reject) => {
		uni.request({
			url: config.host + options.url,
			method: options.method || 'GET',
			header: {
				"access-token": '8ffb0804-f2eb-459e-9fdf-c23b2390e2d5'
				// accessToken
			},
			data: options.data || {},
			success: (res) => {
				if (res.data.code == 'L10002') {
					https({
						url: '/login/refreshAccessToken?refreshAccessToken=' + refToken,
						method: "POST",
					}).then(res => {
						if (res.code == 1) {
							uni.setStorageSync('accessToken', res.data.accessToken);
							const token = uni.getStorageSync('accessToken') || '';
							uni.request({
								url: config.host + options.url,
								method: options.method || 'GET',
								header: {
									"access-token": token
								},
								data: options.data || {},
								success: res => {
									resolve(res.data)
								},
								fail: err => {
									reject(err)
								}
							})
						} else {
							uni.redirectTo({
								url: '/pages/mine/login'
							})
						}
					})
				}
				resolve(res.data)
			},
			fail: (err) => {
				reject(err)
			}
		})
	})
}
